﻿using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Model;
using System.Configuration;
using System.Diagnostics;

namespace NCRE学生考试端V1._0
{
    public partial class FrmTeacherLogin : Form
    {
        public FrmTeacherLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //登陆
            UserEntityBll userentitybll = new UserEntityBll();
            UserEntity userinfo = new UserEntity();
            userinfo.userName = txtUsername.Text.Trim();
            userinfo.userPassword = txtPwd.Text.Trim();
            DataTable dt = userentitybll.TeacherLoginByName(userinfo);
            if (dt.Rows.Count > 0)
            {
                frmTeacherManagerMain teacherform = new frmTeacherManagerMain();
                teacherform.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("没有对应的数据，登陆失败");
            }
        }

        private void FrmTeacherLogin_Load(object sender, EventArgs e)
        {
            //palLogin.Visible = true;
            //palServe.Visible = false;
        }
        /// <summary>
        /// 配置数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetServe_Click(object sender, EventArgs e)
        {
            palLogin.Visible = false;
            palServe.Visible = true;
            btnSave.Focus();
            ShowServe();
        }
        /// <summary>
        /// 保存数据库配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveServe();
            //重新加载程序
            Application.ExitThread();//退出当前线程上的消息循环，并关闭该线程上的所有窗口
            Application.Exit();//停止在所有线程上运行的所有消息循环，并关闭应用程序的所有窗口
            Application.Restart();//系统自动重启
            Process.GetCurrentProcess().Kill();//关闭进程
            btnLogin.Focus();
        }
        /// <summary>
        /// 查询配置文件
        /// </summary>
        void ShowServe()
        {
            string connStr = GetStrCon("connStr");
            string[] str = connStr.Split(new char[2] { '=', ';' });
            txtSQLServe.Text = str[1];
            txtSQLUid.Text = str[3];
            txtSQLPWd.Text = str[5];
            txtSQLname.Text = str[7];
            txtMongoServe.Text = GetStrCon("mongodb");
            txtMongoName.Text = GetStrCon("NCREdb");
        }
        /// <summary>
        /// 更新配置文件
        /// </summary>
        void SaveServe()
        {
            string a = string.Format("server={0};uid={1};pwd={2};database={3}", txtSQLServe.Text, txtSQLUid.Text, txtSQLPWd.Text, txtSQLname.Text);
            UodateConfig("connStr", a, "System.Data.SqlClient");
            UodateConfig("mongodb", txtMongoServe.Text, "System.Data.SqlClient");
            UodateConfig("NCREdb", txtMongoName.Text, "System.Data.SqlClient");
        }
        /// <summary>
        /// 更新连接字符串
        /// </summary>
        /// <param name="newName">连接字符串名称</param>
        /// <param name="newConString">连接字符串内容</param>
        /// <param name="newProviderName">数据提供程序名称</param>
        public static void UodateConfig(string newName, string newConString, string newProviderName)
        {
            //指定config文件读取
            string file = Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            //记录该连接串是否已经存在  
            bool exist = config.ConnectionStrings.ConnectionStrings[newName] != null;
            // 如果连接串已存在，首先删除它  
            if (exist)
            {
                config.ConnectionStrings.ConnectionStrings.Remove(newName);
            }
            //新建一个连接字符串实例  
            ConnectionStringSettings mySettings =
                new ConnectionStringSettings(newName, newConString, newProviderName);
            // 将新的连接串添加到配置文件中.  
            config.ConnectionStrings.ConnectionStrings.Add(mySettings);
            // 保存对配置文件所作的更改  
            config.Save(ConfigurationSaveMode.Modified);
            // 强制重新载入配置文件的ConnectionStrings配置节  
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }
        /// <summary>
        /// 读取动态config文件
        /// </summary>
        /// <param name="StrName">连接串名字</param>
        /// <returns></returns>
        public static string GetStrCon(string StrName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            string connStr = config.ConnectionStrings.ConnectionStrings[StrName].ConnectionString.ToString();
            //string connStr = ConfigurationManager.ConnectionStrings[StrName].ConnectionString.ToString();
            return connStr;
        }

        /// <summary>
        /// palServe返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            palLogin.Visible = true; ;
            palServe.Visible = false;
            btnLogin.Focus();
        }       
    }
}
